/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;

import java.util.Vector;

import com.saville.debug.*;
import com.saville.msgcomp.*;
import com.saville.mc.*;



public class TestAjaxServer1 extends ActiveMc {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new TestAjaxServer1();
    }
    
    TestAjaxServer1() {
        super("TestAjaxServer1");
        df = Log.VERBOSE;
        // mMcMgr.df = Log.VERBOSE;
        TcpMsgPipeConnectionServer tmpcs = new TcpMsgPipeConnectionServer(12345, this);

        if (!tmpcs.start()) {
            throw new RuntimeException("Could not start connection server");
        }
    }

    @Override
    public void processMsg(Msg pipeMsg) {
        Log.d(df, "%s: processMsg() E pipeMsg guid=0x%x cmd=0x%x", mName, pipeMsg.guid, pipeMsg.cmd);

        if (pipeMsg.is(McConst.MC_GUID, McConst.MC_CMD_STARTED)) {
            Log.v(df, "%s: MC_CMD_STARTED", mName);
        } else if (pipeMsg.is(McConst.MCPIPE_GUID, McConst.MCPIPE_CMD_CONNECTED)) {
            /*
             * Create a new pipe for this socket
             */
            Log.v(df, "%s: MCPIPE_CMD_CONNECTED", mName);
            mSocket = (Socket) pipeMsg.obj;
            mAmp = new AjaxMsgPipe(mSocket, this.mMcId.id, 1);
        } else if (pipeMsg.is(McConst.MCPIPE_GUID, McConst.MCPIPE_CMD_NON_AMP_CMD)) {
            Log.v(df, "%s: MCPIPE_CMD_NON_AMP_CMD", mName);
            HtmlReq req = null;
            try {
                req = (HtmlReq)pipeMsg.obj;
                String req_uri = req.getHeader("REQ_URI");
                Log.v(df, "%s: MCPIPE_CMD_NON_AMP_CMD req_uri=%s", mName, req_uri);
                
                String fileName = "/home/wink" + req_uri;
                Log.v(df, "%s: MCPIPE_CMD_NON_AMP_CMD fileName=%s", mName, fileName);
                byte fileData[] = readFile(fileName);
                req.sendResp(200, "text/html", null, fileData);
            } catch (IOException e) {
                try {
                    req.sendResp(404);
                } catch (IOException ee) {
                    Log.e(df, "%s: COUld not send 404", mName);
                }
            }
        } else if (pipeMsg.is(McConst.MCPIPE_GUID, McConst.MCPIPE_CMD_CLOSED)) {
            Log.d(df, "%s: processMsg() remove dstSubId=0x%x", mName, pipeMsg.dstSubId);
            //mSocket.close();
        } else if (pipeMsg.is(McConst.MCPIPE_GUID, McConst.MCPIPE_CMD_PRODUCER_MSG)) {
            Log.d(df, "%s: processMsg() remove dstSubId=0x%x", mName, pipeMsg.dstSubId);
        } else {
            if (pipeMsg.cmdIsRequest()) {
                Log.e(df, "%s: received unknown pipeMsg REQ guid=0x%x cmd=0x%x", mName, pipeMsg.guid,
                    pipeMsg.cmd);
                sendReplyMsg(pipeMsg, McConst.STATUS_UNKNOWN_CMD);
            } else {
                Log.e(df, "%s: received unknown pipeMsg CMD guid=0x%x cmd=0x%x", mName, pipeMsg.guid,
                    pipeMsg.cmd);
            }
        }

        Log.d(df, "%s: processMsg() X", mName);
    }
    
    private byte [] readFile(String fileName) throws IOException {
        File f = new File(fileName);
        FileInputStream fis = new FileInputStream(f);
        int len = (int)f.length();
        byte b[] = new byte[len];
        fis.read(b);
        return b;
    }

    AjaxMsgPipe mAmp;
    Socket mSocket;
}
